#include "QxPLFilter.h"

QxPLFilter::QxPLFilter() : QxPLMessage()
{
    setHop(0);
}

bool QxPLFilter::init(const QString &strfilter)
{
    //syntax, according to xPL core documentation:
    //filter = [msgtype].[vendor].[device].[instance].[class].[type]
    //          0          1        2        3          4       5
    QStringList slist = strfilter.split(".");
    if(slist.count() != 6)
        return false;
    if(slist[0] == "*") setType(ANY);
    else if(slist[0] == "xpl-cmnd") setType(COMMAND);
    else if(slist[0] == "xpl-stat") setType(STATUS);
    else if(slist[0] == "xpl-trig") setType(TRIGGER);
    QxPLIdentifier newFilterSource(slist[1], slist[2], slist[3]);
    setSource(newFilterSource);
    setMessageSchema(slist[4]);
    setMessageClass(slist[5]);
    return true;
}

bool QxPLFilter::match(const QxPLMessage &message) const {
    //compare the types
    if(this->getType() != ANY && message.getType() != this->getType())
        return false;

    //compare hop counts
    /*if(this->getHop() > 0 && message.getHop() > this->getHop())
        return false;*/

    //compare targets
    /*if(!this->getTarget().isBroadcast()) {
        if(this->getTarget().getVendorId() != "*" && message.getTarget().getVendorId() != this->getTarget().getVendorId())
            return false;
        if(this->getTarget().getDeviceId() != "*" && message.getTarget().getDeviceId() != this->getTarget().getDeviceId())
            return false;
        if(this->getTarget().getInstanceId() != "*" && message.getTarget().getInstanceId() != this->getTarget().getInstanceId())
            return false;
    }*/


    //compare sources
    if(!this->getSource().isBroadcast()) {
        if(this->getSource().getVendorId() != "*" && message.getSource().getVendorId() != this->getSource().getVendorId())
            return false;
        if(this->getSource().getDeviceId() != "*" && message.getSource().getDeviceId() != this->getSource().getDeviceId())
            return false;
        if(this->getSource().getInstanceId() != "*" && message.getSource().getInstanceId() != this->getSource().getInstanceId())
            return false;
    }

    //compare xPL schemas
    if(this->getMessageSchema() != "*" && !this->getMessageSchema().isEmpty()
            && this->getMessageSchema() != message.getMessageSchema())
        return false;
    if(this->getMessageClass() != "*" && !this->getMessageClass().isEmpty()
            && this->getMessageClass() != message.getMessageClass())
        return false;

    return true;
}

QString QxPLFilter::toString() const
{
    //syntax, according to xPL core documentation:
    //filter = [msgtype].[vendor].[device].[instance].[class].[type]
    QString rslt = "";
    switch(getType()) {
    case ANY:
        rslt += "*";
        break;
    case COMMAND:
        rslt += "xpl-cmnd";
        break;
    case STATUS:
        rslt += "xpl-stat";
        break;
    case TRIGGER:
        rslt += "xpl-trig";
    case NONE:
        return "";
    }
    rslt += ".";
    rslt += getSource().getVendorId();
    rslt += ".";
    rslt += getSource().getDeviceId();
    rslt += ".";
    rslt += getSource().getInstanceId();
    rslt += ".";
    rslt += getMessageSchema();
    rslt += ".";
    rslt += getMessageClass();
    return rslt;
}
